package com.ihr360.expr.function;

import com.ihr360.expr.ExprException;
import com.ihr360.expr.context.IEvaluationContext;
import com.ihr360.expr.core.Expr;
import com.ihr360.expr.core.ExprFunction;
import com.ihr360.expr.core.IExprFunction;

import java.util.HashMap;
import java.util.Map;

public class FunctionMap implements IFunctionProvider
{
    private boolean caseSensitive;
    private Map<String, IExprFunction> functions = new HashMap();

    public FunctionMap() {
    }

    public FunctionMap(boolean caseSensitive) {
        this.caseSensitive = caseSensitive;
    }

    public void add(String name, IExprFunction function) {
        functions.put(caseSensitive ? name : name.toUpperCase(), function);
    }

    @Override
    public Expr evaluate(IEvaluationContext context, ExprFunction function)
            throws ExprException {
        IExprFunction f = functions.get(function.getName());
        if (f != null)
            return f.evaluate(context, function.getArgs());
        return null;
    }

    @Override
    public boolean hasFunction(ExprFunction function) {
        return functions.containsKey(function.getName());
    }
}
